<?
/**
 * Addition to Array Multisort
 *
 * An easy way to sort database-style results.
 * It takes care of creating those intermediate arrays for you before passing 
 * control on to array_multisort().
 * The sorted array is now in the return value of the function instead of 
 * being passed by reference.
 *
 * Usage Example:
 * <code>
 * $sorted = myb_array_orderby($unsorted, 'colum1', SORT_DESC, 'colum2', SORT_ASC);
 * </code>
 * Column2 is only taken into account if there are some identical entries in column1
 * 
 * Based on Post on http://php.net/array_multisort/ 
 * by jimpoz at jimpoz dot com 
 *
 * Additional info about SORT_ASC, SORT_REGULAR.....
 * http://php.net/manual/function.sort.php
*/
function mb_arrayOrderBy(){
	//getting arguments for variable parameters
	$arguments = func_get_args();
	// Take away the Datapart
	$dataarray = array_shift($arguments);
	// Generate the extra arrays for sorting
	foreach ($arguments as $ney => $field) {
		if (is_string($field)) {
			$temp = array();
			foreach ($dataarray as $key => $row)
				$temp[$key] = $row[$field];
			$arguments[$ney] = $temp;
		}
	}
	//Re add the Data Array at the end for perfekt multisort params
	$arguments[] = &$dataarray;
	// Use call_user_func_array as you can't use array_multisort($arguments);
	//Multisort uses references so it changes the original array 
	call_user_func_array('array_multisort', $arguments);
	//split the result from the rest and return it.
	return array_pop($arguments);
}



/**
 * Generate a table from an array.
 * Example Array:
 * $tabledata[] =array("name" =>"klaus",  "vorname"=>"palle",  "Farbe"=>"blau" );
 * $tabledata[] =array("name" =>"paul",   "vorname"=>"dralle", "Farbe"=>"grün" );
 * $tabledata[] =array("name" =>"willi",  "vorname"=>"walle",  "Farbe"=>"gelb" );
 * 
 * Based on a data array and some additional information, 
 * this function generates an table from a two dimensional array.
 * You can add extra arrays for tablehead and tablefooter.
 * 
 * @param array   $tabledata the array containing the actual data.
 * @param string  $basis     the opening tag of the table, it can contain all stuff you like including class and ID definitions.
 * @param int     $autohl    0=do nothing, 1=fetch the headline from keys if not set, 2=same for footer
 * @param array   $headlines Just an array containing the headlines for each colum.
 * @param attay   $footlines Same for footer.
 * 
 * @return string $output    Returns the HTML for the table as string.
 * 
 * Usage Example:
 * <code>
 * echo mb_array2Tab ( $tabledata,
 *                      '<table class="datatab" id="thetable" cellspacing ="0" border="0"><caption>DIE Tolle Tabelle</caption>',
 *                      2,
 *                      array( "Der Name", "The Vorname", "Farbeinstellung"),
 *                      array( "Der Name", "The Vorname", "Farbeinstellung")
 * );
 * </code>
 * 
 * The table contains a full set of CSS classes to allow all kinds of formating.
 * .hlcol_<number>, .focol_<number>, .col_<number>  for all colums 
 * where <number> is the number of the column.
 * 
 * .rowtype_0  and .rowtype_1 for every second row.
 * .coltype_0  and .coltype_1  for every second colum
 * 
*/

function mb_array2Tab($tabledata=array(),$basis='<table border="1">',$autohl=0 ,$headlines=array(),$footlines=array()) {

	$output= $basis."\n";	

	//check if there is any data to display
	if (count($tabledata)==0){return "No data!";}
	if (count($tabledata[0])==0){return "No data in first line";}

	//count columns
	$hcols =  count($headlines);
	$dcols =  count($tabledata[0]);
        $fcols =  count($footlines);

	//Are headlines and data of identical columns
	if ($hcols==0 OR  $hcols == $dcols) {$cols=$dcols;}
	else                                {return "Headline and Data are of uneven length";}

	//Are footlines and data of identical columns
	if ($fcols==0 OR  $fcols == $dcols) {$cols=$dcols;}
	else                                {return "Footline and Data are of uneven length";}


	//Auto Generate headlines from keys.
	if ($autohl!=0 AND $hcols==0){
        	foreach ($tabledata[0] as $key => $value){
                	$headlines[]= $key;
		}
		$hcols =  count($headlines);
		if ($autohl==2 AND $fcols==0) {
			$footlines=$headlines;
			$fcols=$hcols;	
		}
	} 

	//Generate table header row
	if ($hcols!=0) {
		$output.="<thead>\n<tr>"."\n";
		$coltype=0;
		$colcount=0;
		foreach  ($headlines as $value){
			$output.='<th class="hltype_'.$coltype.' hlcol_'.$colcount.'">'.$value.'</th>'."\n";
			if ($coltype==0) $coltype=1;
			else              $coltype=0;
			$colcount++;
		}
		$output.="</tr>\n</thead>"."\n";
	}

	//Generate table footer row
	if ($fcols!=0) {
		$output.="<tfoot>\n<tr>"."\n";
		$coltype=0;
		$colcount=0;
		foreach  ($footlines as $value){
			$output.='<td class="fotype_'.$coltype.' focol_'.$colcount.'">'.$value.'</td>'."\n";
			if ($coltype==0) $coltype=1;
			else              $coltype=0;
			$colcount++;
		}
		$output.="</tr>\n</tfoot>"."\n";
	}


	// Generate other Rows
	$output.="<tbody>"."\n";
	$rowtype=0;
	$rowcount=0;
	$length=count($tabledata)   ;
	foreach  ($tabledata as $key => $outer_value){
		$output.='<tr class="rowtype_'.$rowtype.' ';

		$output.='">'."\n";
		$coltype=0;
		$colcount=0;
		foreach  ($outer_value as $value){
			$output.='<td class="coltype_'.$coltype.' col_'.$colcount.'">'.$value.'</td>'."\n";
			if ($coltype==0)  $coltype=1;
			else              $coltype=0;
						$colcount++;		
		}
		$output.="</tr>"."\n";
		if ($rowtype==0) $rowtype=1;
		else              $rowtype=0;
		$rowcount++;
	}

	
	$output.="</tbody>\n</table>"."\n";

return $output;
}

function mb_sqlresult2Array($result) {

	$myarray=array();
	while($row = mysql_fetch_assoc($result)) {
		$myarray[]= $row;
	}  
	return $myarray;
}






